package com.qingchunlin.springprinciple.controller;

import com.qingchunlin.springprinciple.entity.User;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
@RequestMapping("/beanScope")
public class BeanScopeController {
    @Autowired
    private ApplicationContext applicationContext;

    @Resource(name = "singleUser")
    private User singleUser1;

    @Resource(name = "prototypeUser")
    private User prototypeUser1;

    @Resource(name = "requestUser")
    private User requestUser1;

    @Resource(name = "sessionUser")
    private User sessionUser1;

    @Resource(name = "applicationUser")
    private User applicationUser1;

    //这个代码的结果,可以看到的是user是一个代理对象,所以第二个sout输出的结果是不变的
    //但是第一个sout输出的结果是要变的,因为代理类的toString方法重写的逻辑是按照被代理对象的toString来进行重写的
    //也是是说代理类只会创建一个,但是我一次request之后,代理类中的被代理类会被重新创建
    @RequestMapping("/test")
    public void test() {
        User user = (User) applicationContext.getBean("requestUser");
        System.out.println(user.toString());
        System.out.println(user.getClass().getName() + "@" + Integer.toHexString(user.hashCode()));
    }

    @RequestMapping("/single")
    public String single() {
        User singleUser2 = (User) applicationContext.getBean("singleUser");
        return "属性注入的对象: " + singleUser1 + " context获取的对象: " + singleUser2;
//        return "属性注入的对象: " + System.identityHashCode(singleUser1) + " context获取的对象: " + System.identityHashCode(singleUser2);
    }

    @RequestMapping("/prototype")
    public String prototype() {
        User prototypeUser2 = (User) applicationContext.getBean("prototypeUser");
        return "属性注入的对象: " + prototypeUser1 + " context获取的对象: " + prototypeUser2;
//        return "属性注入的对象: " + System.identityHashCode(prototypeUser1) + " context获取的对象: " + System.identityHashCode(prototypeUser2);
    }

    @RequestMapping("/request")
    public String request() {
        User requestUser2 = (User) applicationContext.getBean("requestUser");
        return "属性注入的对象: " + requestUser1 + " context获取的对象: " + requestUser2;
//        return "属性注入的对象: " + System.identityHashCode(requestUser1) + " context获取的对象: " + System.identityHashCode(requestUser2);
    }

    @RequestMapping("/session")
    public String session() {
        User sessionUser2 = (User) applicationContext.getBean("sessionUser");
        return "属性注入的对象: " + sessionUser1 + " context获取的对象: " + sessionUser2;
//        return "属性注入的对象: " + System.identityHashCode(sessionUser1) + " context获取的对象: " + System.identityHashCode(sessionUser2);
    }

    @RequestMapping("/application")
    public String application() {
        User applicationUser2 = (User) applicationContext.getBean("applicationUser");
        return "属性注入的对象: " + applicationUser1 + " context获取的对象: " + applicationUser2;
//        return "属性注入的对象: " + System.identityHashCode(applicationUser1) + " context获取的对象: " + System.identityHashCode(applicationUser2);
    }
}
